package jp.ac.tohoku.ecei.cl.www.rite;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashMap;
import javax.xml.parsers.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class RITEXML2RITEExample {

    public RITEXML2RITEExample() {}
    
    public RITEExample parse (Element sentEl, String id, String label, String category) {
        NodeList t1NodeList = sentEl.getElementsByTagName("t1");
        Element t1El = (Element) t1NodeList.item(0);
        NodeList t2NodeList = sentEl.getElementsByTagName("t2");
        Element t2El = (Element) t2NodeList.item(0);
        
        String t1Str = null;
        String t2Str = null;
        if (t1El != null) {
            NodeList t1ChildList = t1El.getChildNodes();
        
            for (int i = 0; i < t1ChildList.getLength(); i++) {
                Node t1Node = t1ChildList.item(i);
                if (t1Node.getNodeType() != Node.TEXT_NODE) { continue; }
                String value = t1Node.getNodeValue();
                t1Str = value;
            }
        }
        if (t2El != null) {
            NodeList t2ChildList = t2El.getChildNodes();
            for (int i = 0; i < t2ChildList.getLength(); i++) {
                Node t2Node = t2ChildList.item(i);
                if (t2Node.getNodeType() != Node.TEXT_NODE) { continue; }
                String value = t2Node.getNodeValue();
                t2Str = value;
            }
        }
        RITEExample ex = new RITEExample(id, t1Str, t2Str, label, 0, "");
        ex.setCategory(category);
        return ex;
    }

    public ArrayList<RITEExample> parseXML (InputStream is) throws Exception {
        Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
        Element root = d.getDocumentElement();

        NodeList pairNodeList = root.getElementsByTagName("pair");

        ArrayList<RITEExample> exampleAry = new ArrayList<RITEExample>();
        //System.err.println("pairNodeList.getLength()="+pairNodeList.getLength());
        for (int i = 0; i < pairNodeList.getLength(); i++) {

            // sentenceID
            String pairID = null;
            String label = null;
            String category = null; 
            // parse 
            Node pairNode = pairNodeList.item(i);
            if (pairNode.getNodeType() != Node.ELEMENT_NODE) { continue; }
            Element pairElement = (Element) pairNode;
            NamedNodeMap pairAttrs = pairElement.getAttributes();
            for (int j = 0; j < pairAttrs.getLength(); j++) {
                Node attrNode = pairAttrs.item(j);
                String attrName = attrNode.getNodeName();
                String attrValue = attrNode.getNodeValue();
                if (attrName.equals("id")) {
                    pairID = attrValue;
                } else if (attrName.equals("label")) {
                    label = attrValue;
                } else if (attrName.equals("category")) {
                    category = attrValue;
                } 
            }
            RITEExample example = parse(pairElement, pairID, label, category);
            exampleAry.add(example);
        }
        return exampleAry;
    }
}
